
'''===========1.序列化器，序列化单个书籍对象==========='''''
from book.序列化器＿模型类序列化.serializers import BookInfoSerializer
from book.models import BookInfo

#1.获取书籍对象
book = BookInfo.objects.get(id=1)

#2.创建序列化器，instance，表示要序列化的对象
serializer = BookInfoSerializer(instance=book)

#3．转换数据
print(serializer.data)


'''===========2.序列化器，序列化多个书籍对象==========='''''
from book.序列化器＿模型类序列化.serializers import BookInfoSerializer
from book.models import BookInfo

#1.获取书籍对象
books = BookInfo.objects.all()

#2.创建序列化器，instance，表示要序列化的对象,many=True　表示序列化多个对象
serializer = BookInfoSerializer(instance=books,many=True)
serializer.data
#3．转换数据
print(serializer.data)
'''
结果：
[OrderedDict([('id', 1), ('btitle', '西游记'), ('bpub_date', '2021-04-21'), ('bread', 30), ('bcomment', 39), ('is_delete', False)]),
 OrderedDict([('id', 2), ('btitle', '水浒传'), ('bpub_date', '2021-04-08'), ('bread', 40), ('bcomment', 30), ('is_delete', False)])]

'''



'''===========４.序列化器，序列化英雄对象==========='''''
from book.序列化器＿模型类序列化.serializers import HeroInfoSerializer
from book.models import HeroInfo

#1.获取书籍对象
hero = HeroInfo.objects.get(id=1)

#2.创建序列化器，instance，表示要序列化的对象
serializer = HeroInfoSerializer(instance=hero)

#3．转换数据
print(serializer.data)

'''===========１.序列化器，反序列化书籍对象==========='''''

from book.序列化器＿模型类序列化.serializers import BookInfoSerializer
# 1.准备数据
book_dict = {
    "btitle":"金瓶x-插画版",
    "bpub_date":"2019-01-01",
    "bread":17,
    "bcomment":25
}

#2,创建序列化器，校验
serializer = BookInfoSerializer(data=book_dict)

# serializer.is_valid()

serializer.is_valid(raise_exception=True) #校验不通过，直接报错
# ３．输出
print(serializer.data)



'''===========2.序列化器，反序列化,create保存数据==========='''''

from book.序列化器＿模型类序列化.serializers import BookInfoSerializer
# 1.准备数据
book_dict = {
    "btitle":"金瓶x-插画版",
    "bpub_date":"2019-01-01",
    "bread":50,
    "bcomment":25
}

#2,创建序列化器，校验
serializer = BookInfoSerializer(data=book_dict)

# serializer.is_valid()

serializer.is_valid(raise_exception=True) #校验不通过，直接报错
# ３．输出
serializer.save()


'''===========３.序列化器，反序列化,update更新数据==========='''''

from book.序列化器＿模型类序列化.serializers import BookInfoSerializer
from book.models import BookInfo
# 1.准备数据
book_dict = {
    "btitle":"水浒传",
    "bpub_date":"2019-01-01",
    "bread":50,
    "bcomment":25
}
#要更新的数据
book = BookInfo.objects.get(id=1)
#2,创建序列化器，校验
serializer = BookInfoSerializer(instance=book,data=book_dict)


serializer.is_valid(raise_exception=True) #校验不通过，直接报错
# ３．入库
serializer.save()

''''
======================模型类序列化器，测试序列化=====================================
'''
from book.models import BookInfo
from book.序列化器＿模型类序列化.serializers import BookModelSerializer


# 序列化
# １．获取模型类对象
book = BookInfo.objects.get(id=1)
book.mobile = '17691394303' #因为mobile 是在序列化器中自定义的字段，数据库没有，所以要用必须指定

# ２．创建序列化器对象
serializer = BookModelSerializer(instance=book)

#3.输出
print(serializer.data)


# ======================模型类序列化器，测试反序列化 create添加数据=====================================
from book.序列化器＿模型类序列化.serializers import BookModelSerializer

# 1.准被数据
book_dict = {
    "btitle":"三国演义",
    "bpub_date":"2019-01-02",
    "bread":50,
    "bcomment":25
}

# ２．创建序列化器对象
serializer = BookModelSerializer(data=book_dict)
#3.校验,入库
serializer.is_valid(raise_exception=True)
serializer.save()


# ======================模型类序列化器，测试反序列化 update更新数据=====================================
from book.序列化器＿模型类序列化.serializers import BookModelSerializer
from book.models import BookInfo
# 1.准被数据
book_dict = {
    "btitle":"红楼梦",
    "bpub_date":"2019-01-02",
    "bread":50,
    "bcomment":25
}
# 序列化
# １．获取模型类对象
book = BookInfo.objects.get(id=1)

# ２．创建序列化器对象
serializer = BookModelSerializer(data=book_dict,instance=book)
#3.校验,入库
serializer.is_valid(raise_exception=True)
serializer.save()